package com.verification.code.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.verification.code.sliding.VerifyImageUtil;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;


/*
 * 滑块验证码控制层
 */
@Controller
public class SliderIMageController {
    // 保存横轴位置用于对比，并设置最大数量为10000，多了就先进先出，并设置超时时间为70秒
    public static Cache < String, Integer > cacheg = CacheBuilder.newBuilder().expireAfterWrite(70, TimeUnit.SECONDS)
            .maximumSize(10000).build();


    @RequestMapping("index")
    public String test(HttpServletRequest request, Model model) throws IOException {
        return "index";
    }


    @RequestMapping("getPic")
    public @ResponseBody Map < String, Object > getPic(HttpServletRequest request) throws IOException {
        // 读取图库目录
       //File imgCatalog = new File(ResourceUtils.getURL("classpath:").getPath() + "\\sliderimage\\targets\\");

        //通过文件路径获得File对象
        File imgCatalog = new File(URLDecoder.decode(this.getClass().getClassLoader().getResource("sliderimage\\targets\\").getPath(),"UTF-8"));

        File[] files = imgCatalog.listFiles();
        System.out.println(files.length);
        // 随机选择需要切的图
        int randNum = new Random().nextInt(files.length);
        File targetFile = files[randNum];
        //加水印
        BufferedImage image = ImageIO.read(targetFile);
        BufferedImage targetFileWater=VerifyImageUtil.addWatermark(image);
        // 随机选择剪切模版
        Random r = new Random();
        int num = r.nextInt(6) + 1;
        //File tempImgFile = new File(ResourceUtils.getURL("classpath:").getPath() + "sliderimage\\templates\\" + num + "-w.png");
        File tempImgFile = new File(URLDecoder.decode(this.getClass().getClassLoader().getResource("sliderimage\\templates\\").getPath(),"UTF-8") + num + "-w.png");
        // 根据模板裁剪图片
        try {
            Map < String, Object > resultMap = VerifyImageUtil.pictureTemplatesCut(tempImgFile, targetFileWater);
            // 生成流水号，这里就使用时间戳代替
            String lno = Calendar.getInstance().getTimeInMillis() + "";
            cacheg.put(lno, Integer.valueOf(resultMap.get("xWidth") + ""));
            resultMap.put("capcode", lno);
            // 移除横坐标送前端
            resultMap.remove("xWidth");
            return resultMap;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @RequestMapping("checkcapcode")
    @ResponseBody
    public  Map < String, Object > checkcapcode(@RequestParam("xpos") int xpos, @RequestParam("capcode") String capcode, HttpServletRequest request) throws IOException {
        Map < String, Object > result = new HashMap < String, Object >();
        Integer x = cacheg.getIfPresent(capcode);
        if (x == null) {
            // 超期
            result.put("code", 3);
        }
        else if (xpos - x > 5 || xpos - x < -5) {
            // 验证失败
            result.put("code", 2);
        }
        else {
            // 验证成功
            result.put("code", 1);
            // .....做自己的操作，发送验证码
        }

        return result;
    }
}
